﻿using System.Collections.Generic;
using System.Linq;
using System.Web.DomainServices;
using System.Web.DomainServices.Providers;
using System.Web.Ria;
using System.Web.Security;
using SoftSource.Timecard.Web.Data;
using SoftSource.Timecard.Web.Resources;

namespace SoftSource.Timecard.Web.Services
{
    [EnableClientAccess]    
    [RequiresAuthentication]
    public class ReadOnlyService : LinqToSqlDomainService<ReadOnlyDBDataContext>
    {        
        [RequiresRole("Administrator")]
        public IQueryable<CustomerProjectSummary> GetCustomerProjectSummaries()
        {
            return this.DataContext.CustomerProjectSummaries;
        }

        [RequiresRole("Administrator")]
        public IQueryable<CustomerSummary> GetCustomerSummaries()
        {
            return this.DataContext.CustomerSummaries;
        }

        [RequiresRole("Administrator")]
        public IQueryable<CustomerUserSummary> GetCustomerUserSummaries()
        {
            return this.DataContext.CustomerUserSummaries;
        }

        [RequiresRole("Administrator")]
        public IQueryable<MilestoneSummary> GetMilestoneSummaries()
        {
            return this.DataContext.MilestoneSummaries;
        }

        [RequiresRole("Administrator")]
        public IQueryable<ProjectMemberSummary> GetProjectMemberSummaries()
        {
            return this.DataContext.ProjectMemberSummaries;
        }

        public IQueryable<ProjectCategorySummary> GetProjectCategorySummaries()
        {
            return this.DataContext.ProjectCategorySummaries;
        }

        public IQueryable<ProjectSummary> GetProjectSummaries()
        {
            if (Roles.IsUserInRole(AuthenticationDataResources.AdministratorRole))
            {
                return this.DataContext.ProjectSummaries;
            }

            AuthenticationService authentication = new AuthenticationService();
            var user = authentication.GetUser();

            if (Roles.IsUserInRole(AuthenticationDataResources.ProjectManagerRole))
            {
                var projects = from p in this.DataContext.ProjectSummaries
                               join pm in this.DataContext.ProjectMemberSummaries on p.ProjectID equals pm.ProjectID
                               where pm.UserID == user.UserID || p.ManagerUserID == user.UserID
                               select p;

                return projects.Distinct();
            }

            if (Roles.IsUserInRole(AuthenticationDataResources.ConsultantRole))
            {
                var projects = from p in this.DataContext.ProjectSummaries
                               join pm in this.DataContext.ProjectMemberSummaries on p.ProjectID equals pm.ProjectID
                               where pm.UserID == user.UserID
                               select p;

                return projects.Distinct();
            }

            return new List<ProjectSummary>().AsQueryable();
        }

        public IQueryable<CategorySummary> GetCategorySummaries()
        {
            return this.DataContext.CategorySummaries;
        }
        
        public IQueryable<UserSummary> GetUserSummaries()
        {
            if (Roles.IsUserInRole(AuthenticationDataResources.ProjectManagerRole))
            {
                AuthenticationService authentication = new AuthenticationService();
                var user = authentication.GetUser();

                var users = from u in this.DataContext.UserSummaries
                            join pm in this.DataContext.ProjectMemberSummaries on u.UserID equals pm.UserID
                            join p in this.DataContext.ProjectSummaries on pm.ProjectID equals p.ProjectID
                            where p.ManagerUserID == user.UserID
                            select u;

                return users.Distinct();
            }
            else
            {
                return this.DataContext.UserSummaries;
            }
        }
    }
}


